/*
 * ControlPanel.fx
 *
 * Created on Apr 26, 2009, 6:19:40 PM
 */

package com.dg.gaming.client.fx.gui;

import com.dg.gaming.api.common.Game;
import com.dg.gaming.api.common.IGameOver;
import com.dg.gaming.api.common.msg.MsgGameOver;
import com.dg.gaming.api.common.msg.MsgInvite;
import com.dg.gaming.api.common.msg.MsgOfferDraw;
import com.dg.gaming.api.common.msg.MsgReadyToStart;
import com.dg.gaming.api.common.msg.MsgSitDown;
import com.dg.gaming.api.common.msg.MsgStandUp;
import com.dg.gaming.api.common.Player;
import com.dg.gaming.api.common.Table;
import com.dg.gaming.api.common.util.DGLabels;
import com.dg.gaming.client.fx.gui.AvailableSeats;
import com.dg.gaming.client.fx.gui.FXMain;
import com.dg.gaming.client.TableConnection;
import java.lang.Object;
import java.util.Observable;
import java.util.Observer;
import javafx.ext.swing.SwingToggleButton;
import javafx.scene.CustomNode;
import javafx.scene.layout.HBox;
import javafx.scene.Node;
import Window.JDLayout.DigLayout;
import Window.JDLayout.Item;
import Window.JDLayout.Row;

/**
 * @author dito
 */

public class ControlPanel extends CustomNode,Observer {

    
    public var txt = "";
    var resources = DGLabels.getInstance();
    
    public var conn:TableConnection;
    public var mainframe:FXMain;
    var userid:Long;
    var availableSeats=AvailableSeats{table:mainframe.table};


    var table:Table;
    var player:Player;
    var game:Game;



                   var sitButton= SwingToggleButton {
                        text: resources.get("sit");
                        action: function() { sitDown(); }
                        enabled:false;
                    };
                   var standButton= SwingToggleButton {
                        text: resources.get("stand");
                        action: function() { standUp(); }
                        enabled:false;
                    };
                     var startButton=SwingToggleButton {
                        text: resources.get("start");
                        action: function() { readyToStart(); }
                        enabled:false;
                    };
                    var offerDrawButton = SwingToggleButton {
                        text: resources.get("offer.draw");
                        action: function() { offerDraw(); }
                        enabled:false;
                    };
                     var resignButton=SwingToggleButton {
                        text: resources.get("resign");
                        action: function() { resign(); }
                        enabled:false;
                    };


    public override  function create():Node {
        return
                    DigLayout{
                        	columns:["e:5","215","e:5","200","e:5","40%"]
                            rows:["12"]
                            fixed:false
                            percentualFlag:true
                            margin:0

                            digrows:[

                                Row{

                                 items:[


                                        Item{
                                            valign:"middle"
                                            item: availableSeats
                                        }

                                        Item{
                                            valign:"top"
                                            halign:"left"
                                            item:
                                                HBox{
                                                    spacing:2
                                                    content: [sitButton,standButton,startButton]
                                                }

                                        },
                                        Item{
                                            valign:"top"
                                            halign:"left"
                                            item: HBox{
                                                spacing:2
                                                content: [offerDrawButton,resignButton]
                                                }
                                        }

                                 ]
                                }
                            ]
                    }

                ;
    }

    init{
        setUp(mainframe);
    }

    public function setUp(mainframe:FXMain){
        this.conn=mainframe.conn;
        this.mainframe=mainframe;
        this.table=conn.getTable();
        this.game = table.getGame();
        this.userid = conn.getUserId();
        this.player = this.table.getPlayerList().getPlayer(userid);
        availableSeats=AvailableSeats{player:player,players:this.table.getPlayerList(),table:this.table};
        println(player);
        this.table.getPlayerList().addObserver(this);
        refresh();

    }

    function sitDown():Void {
       // int selectedSeat = availablePlayers.getSelectedSeat();

        // Create table action communications object and send to server
        var commSitDown = new MsgSitDown(availableSeats.selectedSeat);
        conn.send(commSitDown);
    }
    public function refresh():Void {
             
            // Update the actual button
            sitButton.enabled=player.canSit(table);
            //availablePlayers.enabled=player.canSit(table);
            standButton.enabled=player.canStand(table);
            startButton.enabled=player.canStart(table, conn.getGame());
            offerDrawButton.enabled=player.canOfferDrawResign();
            resignButton.enabled=player.canOfferDrawResign();

           /* var labels = DGLabels.getInstance();
            if (player.isSeated() && !player.canStart(table, conn.getGame())) {
                glassPane.display(labels.get("waiting.on.other.players.to.sit"));
            } else if (player.canStart(table, conn.getGame())) {
                glassPane.display(labels.get("click.start.to.begin.game"));
            } else if (player.isReady() && !table.isGamePlaying()) {
                glassPane.display(labels.get(
                        "waiting.on.other.players.to.start"));
            } else {
                glassPane.setVisible(false);
            }*/
       
       
    }
    public override function update(arg0:Observable , arg1:Object ) {
        refresh();
    }

    function standUp():Void {
        // Set the status of the player back to VIEWING.
        var commStandUp = new MsgStandUp();

        conn.send(commStandUp);
    }

    /**
     * Player clicks the start button.
     */
        function readyToStart():Void {
        var commReadyToStart = new MsgReadyToStart();
        conn.send(commReadyToStart);
    }

    /**
     * Offer the user a draw.
     */
        function offerDraw():Void {
        // Create offer draw communications object and send to server.
        var commOfferDraw = new MsgOfferDraw(
                MsgInvite.REQUEST);
        conn.send(commOfferDraw);
    }
    function resign():Void {
        // Player resigns
        var commGameOver = new MsgGameOver
                                   (IGameOver.USER_RESIGNS);
        conn.send(commGameOver);
    }
}
